package com.wx.media;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wx.token.AccessToken;
import com.wx.type.MediaType;
import com.wx.util.Constants;
import com.wx.util.HttpUtil;
import com.wx.util.TokenUtil;

/**
 * 临时素材服务
 * @author su
 *
 */
public class TempMediaService {
	private static Logger log = LoggerFactory.getLogger(TempMediaService.class);
	
	/**
	 * 临时素材上传
	 * @param access_token
	 * @param filename 上传的文件
	 * @return
	 */
	public static String upload(String access_token,String filename){
		String url = Constants.UPLOAD_MEDIA_POST_URL.replace("ACCESS_TOKEN",
				access_token).replace("TYPE", MediaType.IMAGE);
		String res = "";
		try {
			res = HttpUtil.upload(url, filename,null);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
		}
		log.debug(res);
		return res;
	}
	
	
	/**
	 * 临时素材下载
	 * @param access_token
	 * @param path 存放文件路径
	 * @return
	 */
	public static String dowload(String access_token,String media_id,String path){
		String url = Constants.GET_MEDIA_GET_URL.replace("ACCESS_TOKEN",
				access_token).replace("MEDIA_ID", media_id);
		return HttpUtil.dowload(url,path);
	}
	
	public static void main(String[] args) {
//		AccessToken accessToken = TokenUtil.getAccessToken();
		String access_token = "xDMAWJqeJKasYdm-7TJpjGF0-AoKEfF7ENQh3j_giupFtt2o2srDefLh35M553JuHDDXMVQp3U693XNeawrao-4ezYnmPsgODoeNXdZ9OmEGJWeAAAQTG";//accessToken.getAccess_token();
		
		//上传
		String imageFile = "D:/workspace/infoboard/src/chongwu1124.jpg";
//		upload(access_token,imageFile);
		
		//下载
		String media_id = "fE_QgR8ub5MgJj9ND_ZxsZUJB8kv7zuLnedTkXQwn8yf5yM8cjUmevAxCbJUqNU-";
		String filename = dowload(access_token,media_id,"D:/workspace/infoboard/src/");
		System.out.println(filename);
	}
}
